//
//  DMHomeViewController.m
//  DMGME
//
//  Created by ihope99 on 15/8/27.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMHomeViewController.h"
#import "HomeButton.h"
#import "DMMineWalletController.h"
#import "DMAgentManageController.h"
#import "DMStoreInfoController.h"
#import "DMShopManageController.h"
#import "DMInfoManageController.h"
#import "DMChatViewController.h"
#import "ExamineController.h"
#import "DMLogInController.h"
#import "DMSetController.h"
#import "DMStoreManageController.h"
#import "DMDingDanGuanLiViewController.h"
#import "MobClick.h"


@interface DMHomeViewController ()

@end

@implementation DMHomeViewController
{
    CGFloat splaceY;
    CGFloat heiht;
    NSDictionary *_walletDic;
    NSString *_yuENoti;
    UILabel *moneyNum;
}
-(void)viewWillAppear:(BOOL)animated
{
    [self dataRequest2];
    
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"PageOne"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"PageOne"];
}
- (void)viewDidLoad {
    
    UIScrollView * MainScroll  = [[UIScrollView alloc] initWithFrame:self.view.frame];

    self.view = MainScroll;
    _walletDic = [NSDictionary dictionary];
    
    MainScroll.bouncesZoom=YES;
    
    [super viewDidLoad];
    self.title = @"动漫购老板管理系统";
    [self creatBtn];
    MainScroll.contentSize = CGSizeMake(0, KHeight-64 +heiht +0.5 + 10);

    
    //订阅展示视图消息，将直接打开某个分支视图
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentView:) name:@"PresentView" object:nil];
//    //弹出消息框提示用户有订阅通知消息。主要用于用户在使用应用时，弹出提示框
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showNotification:) name:@"Notification" object:nil];

    
}
-(void)presentView:(NSNotification *)text
{
//        NSLog(@"text.userInfo ====%@",text.userInfo);
    
    if ([text.userInfo[@"param"][@"Aty"]  isEqual: @"Message"])//消息页
    {
        DMInfoManageController *infonman = [[DMInfoManageController alloc] init];
        [self.navigationController pushViewController:infonman animated:YES];
        
    }else if ([text.userInfo[@"param"][@"Aty"]  isEqual: @"Agent"])//代理商管理
    {
        DMAgentManageController *agentman = [[DMAgentManageController alloc] init];
         [self.navigationController pushViewController:agentman animated:YES];
    }else if ([text.userInfo[@"param"][@"Aty"]  isEqual: @"Book"])//商品管理
    {
        DMShopManageController *shopman = [[DMShopManageController alloc] init];
        [self.navigationController pushViewController:shopman animated:YES];
    }else if ([text.userInfo[@"param"][@"Aty"]  isEqual: @"Order"])//订单管理
    {
        DMDingDanGuanLiViewController *dingdanman = [[DMDingDanGuanLiViewController alloc] init];
        [self.navigationController pushViewController:dingdanman animated:YES];
    }else//钱包页
    {
        DMMineWalletController *mineman = [[DMMineWalletController alloc] init];
        [self.navigationController pushViewController:mineman animated:YES];
    }
    
   }

- (void)registrBtnClick{
    DMLogInController *logVC = [[DMLogInController alloc] initWithNibName:@"DMLogInController" bundle:nil];
    [self.navigationController pushViewController:logVC animated:YES];
}

- (void)sizeText {
//    CGFloat splaceH;
    
    if (IsiPhone6||IsiPhone6Plus) {
        splaceY = 45;
    }else if (IsiPhone5){
        splaceY = 35;
    }else{
        splaceY = 15;
    }
}
- (void)creatBtn{

    NSArray *arrText = @[@"我的钱包",@"代理商管理",@"店铺信息",@"商品管理",@"信息管理",@"订单管理",@"设置",@""];
    
    CGFloat hheith = KHeight-64-50-13;
    heiht = (hheith -20)/3;

    
    UIView *backViewGrongde = [[UIView alloc] initWithFrame:CGRectMake(13, 13, KWidth-26,hheith + 10 +heiht)];
//    backViewGrongde.backgroundColor = [UIColor grayColor];
   
    int m = 0;
    for (int i = 0; i <=3; i++) {
        for (int j = 0; j <=1; j++) {
//         heiht = (hheith -20)/3;
        CGFloat weiht = (WIDTH(backViewGrongde)-10)/2;
            
        HomeButton *homeBtn = [[HomeButton alloc] initWithFrame:CGRectMake(j*(weiht+10), i*(heiht+10),weiht,heiht)];
        homeBtn.tag = ++m;
            homeBtn.backgroundColor = ComColor;
            homeBtn.lable.text = arrText[m-1];
            UIImageView *ceshiView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"homeBtnImage%d",m]]];
            [homeBtn addSubview:ceshiView];
            
            [self sizeText];
           
            ceshiView.center = CGPointMake(weiht/2, splaceY + HEIGHT(ceshiView)/2);
           
            if (m == 1) {
                moneyNum = [[UILabel alloc] init];
                moneyNum.frame = CGRectMake(36, CGRectGetMaxY(homeBtn.lable.frame)+5, WIDTH(homeBtn)-72, 15);
                moneyNum.textColor = [UIColor whiteColor];
                moneyNum.textAlignment = NSTextAlignmentCenter;
//                NSString *str1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"order"];
//                NSString *str2 =[[NSUserDefaults standardUserDefaults] objectForKey:@"subsidy"];
//                NSLog(@"==%@",str2);
//                float sum = str1.floatValue +str2.floatValue;
                moneyNum.text = [NSString stringWithFormat:@"0元"];
//              NSLog(@"==%@",_walletDic[@"balance"]);
                moneyNum.font = [UIFont systemFontOfSize:13];
                [homeBtn addSubview:moneyNum];
            }
            
            if (m == 8) {
                homeBtn.hidden = YES;
            }
        
            
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnClike:)];
        [homeBtn addGestureRecognizer:tgr];
           
            [backViewGrongde addSubview:homeBtn];

        }
    }
    [self.view addSubview:backViewGrongde];
    
    UILabel *phoneNum = [[UILabel alloc] initWithFrame:CGRectMake(60, CGRectGetMaxY(backViewGrongde.frame)  +10, KWidth-120, 20)];
    phoneNum.text = @"客服电话:0519-88138239";
    phoneNum.textColor = [UIColor grayColor];
    phoneNum.textAlignment = NSTextAlignmentCenter;
//    phoneNum.backgroundColor = [UIColor grayColor];
    [self.view addSubview:phoneNum];
}
//全部金额
-(void)dataRequest2
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[AFDownManager MD5String:@"%^&*dongmangoug!@#$"] forKey:@"token"];
    [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    
    [AFDownManager POST:MyAllMoney parameters:dict success:^(id responseObject) {
//        NSLog(@"全部金额==%@",responseObject[@"data"]);
        _walletDic = responseObject[@"data"];
      moneyNum.text = [NSString stringWithFormat:@"%@元",_walletDic[@"balance"]];
    } failure:^(NSError *error) {
       
        [self showMsg:@"网络连接故障"];
//        NSLog(@"dataRequest error = %@",error);
        
    }];
    
}

- (void)btnClike:(UITapGestureRecognizer *)sender{
    UIView *view = sender.view;
//    NSLog(@"lalala%ld",(long)view.tag);
    if (view.tag == 1) {
        DMMineWalletController *vc1 = [[DMMineWalletController alloc] init];
//       [self showMsg:@"抱歉,该功能正在开发中，请期待后续版本..."];
        
       [self.navigationController pushViewController:vc1 animated:YES];
    }else if (view.tag == 2){
        DMAgentManageController *vc2 = [[DMAgentManageController alloc] init];
        [self.navigationController pushViewController:vc2 animated:YES];
    }else if (view.tag == 3){
        DMStoreManageController *vc3 = [[DMStoreManageController alloc] init];
        [self.navigationController pushViewController:vc3 animated:YES];
    }else if (view.tag == 4){
        DMShopManageController *vc4 = [[DMShopManageController alloc] init];
        [self.navigationController pushViewController:vc4 animated:YES];
//        [self showMsg:@"抱歉,该功能正在开发中，请期待后续版本..."];

    }else if (view.tag == 5){
       DMInfoManageController *vc5 = [[DMInfoManageController alloc] init];
//        [self showMsg:@"抱歉,该功能正在开发中，请期待后续版本..."];
        [self.navigationController pushViewController:vc5 animated:YES];

    }else if (view.tag == 6){
//        DMOrderManViewController *vc6 = [[DMOrderManViewController alloc] init];
//        [self.navigationController pushViewController:vc6 animated:YES];
////        [self showMsg:@"抱歉,该功能正在开发中，请期待后续版本..."];
        
        DMDingDanGuanLiViewController *vc61 = [[DMDingDanGuanLiViewController alloc] init];
        [self.navigationController pushViewController:vc61 animated:YES];

    }else if (view.tag == 7){
        
        DMSetController *vc6 = [[DMSetController alloc] init];
        [self.navigationController pushViewController:vc6 animated:YES];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
